package ua.com.jpy.controllers.register;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import ua.com.jpy.entity.customer.Customer;
import ua.com.jpy.entity.role.Role;
import ua.com.jpy.services.customer.ICustomerService;

/**
 * @author LSD25
 *
 */
@Controller
@SessionAttributes({ "customer" })
@RequestMapping(value = "/registration")
public class RegisterController {
	
	private static final Log log = LogFactory.getLog(RegisterController.class);
	
	private static final String ROLE_USER = "ROLE_USER";
	
//	private static final String ROLE_ADMIN = "ROLE_ADMIN";
	
	@Autowired
	private ICustomerService customerService;
	
	@RequestMapping(value = { "", "/", "*" }, method = RequestMethod.GET)
	public String registerPage(ModelMap modelMap) {
		return "registration";
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	@RequestMapping(value = { "", "/", "*" }, method = RequestMethod.POST)
	public @ResponseBody JSONObject register(ModelMap modelMap, @ModelAttribute Customer customer, 
			@RequestBody @RequestParam(value = "userName") String name, @RequestBody @RequestParam(value = "userPass") String password, 
			@RequestBody @RequestParam(value = "userPassConf") String passConfirm) {
//		ModelAndView model = new ModelAndView("test");
//		if(name == null || name.isEmpty()) {
//			model.addObject("errorMessage", "The name is empty");
//			return model;
//		}
//		if(password == null || password.isEmpty()) {
//			model.addObject("errorMessage", "The password is empty");
//			return model;
//		}
//		if(passConfirm == null || passConfirm.isEmpty()) {
//			model.addObject("errorMessage", "The password confirm is empty");
//			return model;
//		}
//		if(password.length() < 8) {
//			model.addObject("errorMessage", "Password is short, 8 min symbol");
//			return model;
//		}
//		if(password.length() > 25) {
//			model.addObject("errorMessage", "Password is long, 25 max symbol");
//			return model;
//		}
//		if(passConfirm.length() < 8) {
//			model.addObject("errorMessage", "Password confirm is short, 8 min symbol");
//			return model;
//		}
//		if(passConfirm.length() > 25) {
//			model.addObject("errorMessage", "Password confirm is long, 25 max symbol");
//			return model;
//		}
//		if(!password.equals(passConfirm)) {
//			model.addObject("errorMessage", "Password not equals");
//			return model;
//		}
		if(customer != null && customerService.validate(customer).isEmpty()) {
			log.info("This cutomer is register");
			JSONObject json = new JSONObject();
			json.put("result", "success");
			return json;
		} else {
			log.info("Create new customer");
			customer = new Customer();
			customer.setId(new ObjectId());
			customer.setLogin(name);
			customer.setPassword(password);
	
			Role role = new Role();
			role.setRole(ROLE_USER);
			List<Role> roles = new ArrayList<Role>();
			roles.add(role);
			
			customer.setRoles(roles);
			customer.setActive(true);
			customer.setDateRegistration(new Date());
			
			JSONObject responseJSON = customerService.validate(customer);
			
			customerService.getOrCreate(customer, null);
			log.info("New customer with name: " + name + " was succesfully created!!!");
			modelMap.addObject("successMessage", "Customner is successfuly created");
			return responseJSON;
		}
	}
	
}
